package photobox.manage

import grails.converters.JSON
import photobox.BaseController
import photobox.exception.NotFoundException
import photobox.utils.ParamUtils
import photobox.utils.ResultUtil

class MngAdministratorController extends BaseController {
    def  administratorService

    def index() {}

    def list() {
        def searchParams = ParamUtils.parseFilter(params.filters)
        def pageParams = ParamUtils.parsePage(params)
        def sortParams = ParamUtils.parseSort(params,
                ["id", "username", "nickName", "email"], [sort: 'id', order: "desc"])

        def userRs = administratorService.listAdmin(searchParams, pageParams, sortParams)
        render ResultUtil.pageResults(userRs.rs, userRs.total, pageParams,
                [id: 'id', username: 'username', nickName: 'nickName', enabled: 'enabled', realName: 'realName']) as JSON
    }

    def create() {

    }

    def save() {
        administratorService.createAdmin(params)
        render([error: 0, message: "创建新管理员完成"] as JSON)
    }

    def edit() {
        def admin = Administrator.get(params.id as Long)
        if (!admin) {
            throw new NotFoundException("没有找到对应的管理员")
        }
        return [admin: admin]
    }

    def update() {
        administratorService.updateAdmin(params)
        render([error: 0, message: "更新管理员信息完成"] as JSON)
    }

    def delete() {
        administratorService.deleteAdmin(params)
        render([error:0, message: "删除管理员完成"] as JSON)
    }
}
